//
//  CircleTopicViewController.m
//  hospitalVIP
//
//  Created by xiaoyan on 14-3-9.
//  Copyright (c) 2014年 MEETStudio. All rights reserved.
//

#import "CircleTopicViewController.h"

#import "TopicDetailViewController.h"

@interface CircleTopicViewController (){
    
    NSMutableArray *topics;
}

@end

@implementation CircleTopicViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)dealloc{
    
    [topics release];
    
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.rowHeight = 100;
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"bg_pattern_wood" ofType:@"png"];
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:path]];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    
    self.title = @"话题";
    
    //ios7 优化
    if( iOS7) {
        
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.navigationController.navigationBar.translucent = NO;
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
        
    }
    else{
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
    }
    
    
    UIButton *leftBt = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [leftBt setImage:[UIImage imageNamed:@"icon_return.png"] forState:UIControlStateNormal];
    [leftBt addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:leftBt];
    [self.navigationItem setLeftBarButtonItem:leftButton];
    [leftBt release];
    [leftButton release];
    
    UIButton *rightBt = [[UIButton alloc]initWithFrame:CGRectMake(0, 7, 50, 30)];
    [rightBt.titleLabel setFont:[UIFont systemFontOfSize:17]];
    [rightBt.titleLabel setTextColor:[UIColor whiteColor]];
    [rightBt setTitle:@"发起" forState:UIControlStateNormal];
    [rightBt addTarget:self action:@selector(sendTopic) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:rightBt];
    [self.navigationItem setRightBarButtonItem:rightButton];
    [rightBt release];
    [rightButton release];

    
    
    //
    NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"testicon7.jpg",@"picurl",@"指责孩子前请了解心智理论",@"title",@"395",@"commentcount",@"205",@"personcount", nil];
    NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"testicon8.jpg",@"picurl",@"宝宝湿疹全攻略",@"title",@"195",@"commentcount",@"25",@"personcount", nil];
    NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:@"testicon9.jpg",@"picurl",@"孕妇体型恢复指导",@"title",@"225",@"commentcount",@"95",@"personcount", nil];
    NSDictionary *dict4 = [NSDictionary dictionaryWithObjectsAndKeys:@"testicon10.jpg",@"picurl",@"糖尿病孕妇注意事项",@"title",@"123",@"commentcount",@"25",@"personcount", nil];
    
    topics = [[NSMutableArray alloc]initWithObjects:dict1,dict2,dict3,dict4, nil];


    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)backAction:(id)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)sendTopic{
    
    RecordViewController *control = [[RecordViewController alloc]init];
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:control];
    [self presentViewController:nav animated:YES completion:nil];
    [control release];
    [nav release];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return topics.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.backgroundColor = [UIColor clearColor];
        
        UIImageView *bgview = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 300,90 )];
        bgview.backgroundColor = [UIColor colorWithRed:192/255.0 green:174/255.0 blue:151/255.0 alpha:1];
        [cell.contentView addSubview:bgview];
        bgview.clipsToBounds = YES;
        [bgview release];
        
        UIImageView *signImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15, 80, 80)];
        signImageView.image = [UIImage imageNamed:@"testicon2.png"];
        signImageView.tag = 7;
        signImageView.clipsToBounds = YES;
        [signImageView setContentMode:UIViewContentModeScaleAspectFill];
        [cell.contentView addSubview:signImageView];
        [signImageView release];
        
        UILabel *nameLable = [[UILabel alloc] initWithFrame:CGRectMake(105, 10, 210, 40)];
        nameLable.tag = 8;
        nameLable.textColor = [UIColor colorWithRed:51.0 / 255.0 green:26.0 / 255.0 blue:3.0 / 255.0 alpha:1.0];
        nameLable.font = [UIFont boldSystemFontOfSize:17];
        nameLable.textAlignment = NSTextAlignmentLeft;
        nameLable.text = @"官方公告";
        [cell.contentView addSubview:nameLable];
        [nameLable release];
        
        
        UIImageView *subicon1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"like_top_iphone.png"]];
        subicon1.frame = CGRectMake(105, 50, 14, 12);
        [cell.contentView addSubview:subicon1];
        [subicon1 release];
        
        UILabel *sublable1 = [[UILabel alloc]initWithFrame:CGRectMake(120, 50, 30, 12)];
        sublable1.font = [UIFont systemFontOfSize:10];
        sublable1.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:sublable1];
        sublable1.text = @"124";
        [sublable1 release];
        
        
        UIImageView *subicon2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"said_top_iphone.png"]];
        subicon2.frame = CGRectMake(155, 50, 14, 12);
        [cell.contentView addSubview:subicon2];
        [subicon2 release];
        
        UILabel *sublable2 = [[UILabel alloc]initWithFrame:CGRectMake(170, 50, 30, 12)];
        sublable2.font = [UIFont systemFontOfSize:10];
        sublable2.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:sublable2];
        sublable2.text = @"479";
        [sublable2 release];
        
        
     
    }
    
    UIImageView *imageView = (UIImageView*)[cell.contentView viewWithTag:7];
    NSString *imagename = [[topics objectAtIndex:indexPath.row] objectForKey:@"picurl"];
    imageView.image = [UIImage imageNamed:imagename];
    
    
    UILabel *namelable = (UILabel*)[cell.contentView viewWithTag:8];
    NSString *name = [[topics objectAtIndex:indexPath.row] objectForKey:@"title"];
    namelable.text = name;

    UILabel *lable1 = (UILabel*)[cell.contentView viewWithTag:10];
    NSString *personcnt = [[topics objectAtIndex:indexPath.row] objectForKey:@"commentcount"];
    lable1.text = [NSString stringWithFormat:@"%@条讨论",personcnt];
    
 
    
    // Configure the cell...
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here, for example:
    // Create the next view controller.
    TopicDetailViewController *detailViewController = [[TopicDetailViewController alloc] initWithNibName:@"TopicDetailViewController" bundle:nil];

    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
}
 


@end
